package cn.jilian.front.service.impl;

import java.io.IOException;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;

import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.github.qcloudsms.httpclient.HTTPException;

import cn.jilian.common.service.SMSService;

@Service
@ConditionalOnProperty(prefix="sms.platform",name="provider",havingValue="tencent")
public class TencentSmsService implements SMSService {

	@Value("${tencent.sms.appId}")
	private Integer appid;
	@Value("${tencent.sms.appKey}")
	private String appkey;
	
	

	@Override
	public int sendSms(String tel, String content) {
			try {
			    SmsSingleSender ssender = new SmsSingleSender(appid, appkey);
			    SmsSingleSenderResult result = ssender.send(0, "86", tel,
			        content, "", "");
			    System.out.println(result);
			    return result.result;
				} catch (HTTPException e) {
					    // HTTP响应码错误
					    e.printStackTrace();
				} catch (JSONException e) {
					    e.printStackTrace();
				} catch (IOException e) {
					    // 网络IO错误
					    e.printStackTrace();
				}
			return -1;
	}

	@Override
	public int sendSmsCode(String tel, String templateId, String code) {
		String[] params = {code};
		Integer tplId=Integer.parseInt(templateId);
		 SmsSingleSender ssender = new SmsSingleSender(appid, appkey);
		try {
			SmsSingleSenderResult result = ssender.sendWithParam("86", tel,
					tplId, params, "", "", "");
			return result.result;
		} catch (JSONException | HTTPException | IOException e) {
			e.printStackTrace();
			return -1;
		}
	}

}
